package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import darksideoftheloom.Attendee;
import darksideoftheloom.Submission;

/**
 * This class shows information for Attendees when they are logged in.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 * 
 */
@SuppressWarnings("serial")
public class BeweaveAttendeePanel extends JPanel {

    /**
     * The width of the panel.
     */
    private static final int WIDTH = 1024;

    /**
     * The height of the panel.
     */
    private static final int HEIGHT = 768;

    /**
     * The index of the submission panel.
     */
    private static final int SPINDEX = 2;

    /**
     * The index of the submit button.
     */
    private static final int SBINDEX = 0;

    /**
     * The number of columns in the submission display table.
     */
    private static final int COLS = 2;

    /**
     * The logged in attendee.
     */
    private Attendee my_attendee;

    /**
     * The center panel.
     */
    private final JPanel center_panel = new JPanel();

    /**
     * The bottom panel a.k.a. south panel.
     */
    private final JPanel bottom_panel = new JPanel();

    /**
     * Constructs the panel that will be used to display attendee submissions
     * and information.
     */
    public BeweaveAttendeePanel(final JPanel the_panel) {
        super();
        setLayout(new BorderLayout());

        final JButton submit_button = new JButton(new AbstractAction(
                "Submit...") {
            public void actionPerformed(ActionEvent the_event) {
                ((BeweaveSubmitPanel) the_panel.getComponent(SPINDEX))
                        .setAttendee(my_attendee);
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "submit");
            }
        });
        final JButton logout_button = new JButton(new AbstractAction("Logout") {
            public void actionPerformed(final ActionEvent the_event) {
                my_attendee = null;
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "login");
            }
        });
        bottom_panel.add(submit_button);
        bottom_panel.add(logout_button);
        center_panel.setLayout(new BorderLayout());
        add(bottom_panel, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }

    /**
     * Upon successful login, the user is set here.
     * 
     * @param the_attendee
     *            the Attendee that is logged in
     */
    public void login(final Attendee the_attendee) {
        my_attendee = the_attendee;
        showSubmissions();
    }

    /**
     * Populates the panel with information.
     */
    public void showSubmissions() {
        center_panel.removeAll();

        String[] column_names = { "Category", "Title" };
        // TODO get rid of hardcoded values
        Object[][] data = new Object[3][COLS];
        int number_of_submissions = my_attendee.getSubmissions().size();
        for (int i = 0; i < number_of_submissions; i++) {
            Submission s = my_attendee.getSubmissions().get(i);
            data[i][0] = s.getCategory();
            data[i][1] = s.getDesignName();
        }
        JTable table = new JTable(data, column_names);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        center_panel.add(scrollPane, BorderLayout.CENTER);

        add(center_panel, BorderLayout.CENTER);

        if (number_of_submissions < 3) {
            bottom_panel.getComponent(SBINDEX).setEnabled(true);
        } else {
            bottom_panel.getComponent(SBINDEX).setEnabled(false);
        }
    }
}
